//
//  GDMinusView.m
//  美团试验 (自己)
//
//  Created by 热带雨林 on 17/2/13.
//  Copyright © 2017年 热带雨林. All rights reserved.
//

#import "GDMinusView.h"
@interface GDMinusView()
@property (weak, nonatomic) IBOutlet UIButton *plusButton;
@property (weak, nonatomic) IBOutlet UILabel *countLabel;

@end
@implementation GDMinusView

- (void)setMinusNum:(NSInteger)minusNum{
    _minusNum = minusNum;
    
    _plusButton.hidden = (minusNum == 0);
    _countLabel.hidden = (minusNum == 0);
    
    _countLabel.text = @(minusNum).description;
}

- (IBAction)plusButton:(UIButton *)sender {
    
    _isPlus = NO;
    self.minusNum --;
    
    if([_delegate respondsToSelector:@selector(minusView:withMinusNum:)]){
        [_delegate minusView:self withMinusNum:self.minusNum];
    }
}

- (IBAction)addButton:(UIButton *)sender {
    
    _isPlus = YES;
    
    self.minusNum ++;
    
    //获得window
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    //转移动画的起点,将动画的起点添加到window中
    _startPoint = [self convertPoint:sender.center toView:keyWindow];
    //转移

    if([_delegate respondsToSelector:@selector(minusView:withMinusNum:)]){
        [_delegate minusView:self withMinusNum:self.minusNum];
    }
}

//使用类方法添加XIB视图
+ (instancetype)minusView{
    
    UINib *nib = [UINib nibWithNibName:@"GDMinusVIew" bundle:nil];
    
    return [[nib instantiateWithOwner:nil options:nil]lastObject];
    
}

- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    self.minusNum = 0;
}
@end
